/**
******************************************************************************
* @file    			bsp_tim.c
* @version 			V1.0
* @date    			2024-08-21
* @brief 		    wizchip tim initialization
*
* @company  		WIZnet
* @website  		https://wiznet.io/
* @gitee            https://gitee.com/wiznet-hk
* @github           https://github.com/Wiznet/
******************************************************************************
*/

#include "bsp_tim.h"
#include "dhcp.h"
#include "dns.h"
#include "mqtt_interface.h"
#include "w55mh32.h"
#include "wiz_interface.h"

/**
 * @brief   1ms timer init
 * @param   none
 * @return  none
 */
volatile uint32_t g_system_ticks = 0;
void tim3_init(void)
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    RCC_ClocksTypeDef clocks;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); // Clock enablement
    RCC_GetClocksFreq(&clocks);

    TIM_TimeBaseStructure.TIM_Period =
        1000 - 1; // Automatic reloading cycle value
    TIM_TimeBaseStructure.TIM_Prescaler =
        clocks.HCLK_Frequency / 1000000 - 1; // Clock frequency pre division value
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode =
        TIM_CounterMode_Up; // Up counting mode
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

    TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); // Enable update interrupt

    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    TIM_Cmd(TIM3, ENABLE);
}

/**
 * @brief   1ms timer IRQ Handler
 * @param   none
 * @return  none
 */
void TIM3_IRQHandler(void)
{
    static uint32_t tim3_1ms_count = 0;
    if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
    {
        // ����ϵͳ�δ������
        g_system_ticks++;
        tim3_1ms_count++;
        MilliTimer_Handler();
        if (tim3_1ms_count >= 1000)
        {
            DHCP_time_handler();
            DNS_time_handler();
            tim3_1ms_count = 0;
        }
        TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
    }
}

// ���ӷ�������ʱ����
void non_blocking_delay(uint32_t *start_time, uint32_t delay_ms)
{
    if (*start_time == 0)
    {
        // �״ε��ã���¼��ʼʱ��
        *start_time = g_system_ticks;
    }
    // ��ʱδ��ʱ��ִ�в���
    while ((g_system_ticks - *start_time) < delay_ms)
    {
        // ��ѭ�����ȴ�ʱ�䵽��
    }
    // ��ʱ���������ü�ʱ��
    *start_time = 0;
}
